package jdk8.stream;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 排序演示
 */
public class SortDemo {
    public static void main(String[] args) {
        //test1();
        //test2();
        test3();
    }

    public static void test1(){
        //String,Integer 这些包装类都实现了Comparable，不传排序规则是会按照Comparable实现进行排序
        Stream.of(3, 1, 4, 5, 9, 7).sorted().forEach(f->{
            System.out.println(f);
        });
    }

    public static void test2(){
        LocalDate now = LocalDate.now();
        Stream.of(now.plusDays(3),now.plusDays(1) , now.plusDays(4), now.plusDays(5), now.plusDays(9), now.plusDays(7)).sorted().forEach(f->{
            System.out.println(f);
        });
    }

    public static void test3(){
        User user1 = new User(1, 3);
        User user2 = new User(2, 1);
        User user3 = new User(3, 4);
        User user4 = new User(4, 5);
        User user5 = new User(5, 9);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        List listRet=list.stream().sorted((u1, u2) -> {
            return u1.getAge() > u2.getAge()?1:-1;
        }).collect(Collectors.toList());
        //原数组并没有变
        list.forEach(f->{
            System.out.println(f);
        });
        System.out.println("----------------------------------------------");
        listRet.forEach(f->{
            System.out.println(f);
        });
    }

}


